package com.db.springcloud.controller;


import com.db.springcloud.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserConsumerController {

    //消费者不应有service层
    //RestTemplate直接调用，注册到spring中
    //(url,实体:map,Class<T>ResponseType)
    @Autowired
    private RestTemplate restTemplate;//提供多种便捷访问远程http服务的方法，简单的restful服务模板

    //ribbon这里的地址应该是一个变量
//    private static final String REST_URL_PREFIX = "http://localhost:8001";
    private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-USER";

    @RequestMapping("/consumer/user/add")
    public boolean addUser(User user){
        return restTemplate.postForObject(REST_URL_PREFIX + "/user/add","",Boolean.class,user);
    }

    @RequestMapping("/consumer/user/get/{id}")
    public User queryById(@PathVariable("id") int id){
        return restTemplate.getForObject(REST_URL_PREFIX+"/user/get/"+id,User.class,id);
    }

    @RequestMapping("/consumer/user/getall")
    public List<User> queryAll(){
        return restTemplate.getForObject(REST_URL_PREFIX+"/user/getall",List.class);
    }

    @RequestMapping("/consumer/user/getbyid")
    public User getById(int id){
        return restTemplate.getForObject(REST_URL_PREFIX+"/user/getbyid?id={1}",User.class,id);
    }

}
